package cn.thornbird.orgsync.helper.converter;

import cn.thornbird.orgsync.helper.AppHelper;
import cn.thornbird.orgsync.model.AppDepartmentData;
import cn.thornbird.orgsync.model.AppUserData;
import cn.thornbird.orgsync.model.feishu.DepartmentList;
import cn.thornbird.orgsync.model.feishu.UserInfo;
import cn.thornbird.orgsync.model.feishu.UserList;

import java.util.HashMap;

public class FeishuConverter extends DataConverter<DepartmentList.Department, UserList.User, UserInfo> {

    @Override
    public AppDepartmentData parseDepartment(DepartmentList.Department department) {
        AppDepartmentData departmentData = new AppDepartmentData();
        departmentData.setId(department.getDepartmentId());
        departmentData.setParentId(department.getParentDepartmentId());
        departmentData.setName(department.getName());
        if (department.getI18nName() != null && department.getI18nName().getEnUs() != null) {
            departmentData.setEnglishName(department.getI18nName().getEnUs());
        }
        departmentData.setOrder(department.getOrder() == null ? null : Long.parseLong(department.getOrder()));
        return departmentData;
    }

    @Override
    public AppUserData parseUser(UserList.User user) {
        if (user == null) {
            return null;
        }

        AppUserData userData = new AppUserData();
        userData.setId(user.getUserId());
        userData.setEmployeeNo(user.getEmployeeNo());
        userData.setName(user.getName());
        userData.setAvatar(user.getAvatar() == null ? null : user.getAvatar().getAvatar72());
        userData.setGender(user.getGender());
        userData.setMobile(user.getMobile());
        userData.setEmail(user.getEmail());
        userData.setTitle(user.getJobTitle());
        userData.setLeaderId(user.getLeaderUserId());

        if (user.getDepartmentIds() != null) {
            userData.setDepartments(new HashMap<>());
            int count = user.getOrders() == null ? 0 : user.getOrders().size();
            for (int i = 0; i < count; i++) {
                UserList.UserOrder order = user.getOrders().get(i);
                if (order.getIsPrimaryDepartment() != null && order.getIsPrimaryDepartment()) {
                    userData.setMainDepartmentId(order.getDepartmentId());
                }
            }
            for (String departmentId : user.getDepartmentIds()) {
                AppUserData.DepartmentInfo departmentInfo = new AppUserData.DepartmentInfo();
                for (int i = 0; i < count; i++) {
                    UserList.UserOrder order = user.getOrders().get(i);
                    if (order.getDepartmentId().equals(departmentId)) {
                        if (order.getUserOrder() != null) {
                            departmentInfo.setOrder(order.getUserOrder().longValue());
                        }
                        break;
                    }
                }
                userData.getDepartments().put(departmentId, departmentInfo);
            }
        }

        return userData;
    }

    @Override
    public UserInfo toUserInfo(AppUserData userData) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userData.getId());
        userInfo.setEmployeeNo(userData.getEmployeeNo());
        userInfo.setName(userData.getName());
        userInfo.setGender(userData.getGender());
        userInfo.setMobile(AppHelper.getPureMobile(userData.getMobile()));
        userInfo.setEmail(userData.getEmail());
        userInfo.setJobTitle(userData.getTitle());
        userInfo.setLeaderUserId(userData.getLeaderId());
        userInfo.setDepartments(userData.getDepartments(), userData.getMainDepartmentId());
        return userInfo;
    }

}
